package com.link4a.doms.dao.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.link4a.doms.dao.Page;
import com.link4a.doms.dao.SqlSessionTemplateSupport;
import com.link4a.doms.model.License;
import com.link4a.doms.vo.LicenseCondition;

@Repository
public class LicenseHandler extends SqlSessionTemplateSupport {

	
	public License findById(int id) {
		return getSqlSession().selectOne("findById", id);
	}
	
	public List<License> findByDriver(int driverId) {
		return getSqlSession().selectList("findByDriver", driverId);
	}

	public License findByLicense(String license) {
		return getSqlSession().selectOne("findByLicense", license);
	}
	
	public Page<License> findPage(LicenseCondition licenseCondition) {
		return findPage(licenseCondition, "countPage", "findPage");
	}
	
	public void merge(List<License> licenses) {
		for (License license:licenses) {
			license.setStatus(License.STATUS_IDLE);
			getSqlSession().insert("merge", license);
		}
	}
//	
//	public void deleteByOrder(String orderId) {
//		getSqlSession().delete("deleteByOrder", orderId);
//	}
	
//	public void update(License license) {
//		getSqlSession().update("update", license);
//	}
//	
//	public void delete(int id) {
//		getSqlSession().delete("delete", id);
//	}

	public void disable(int id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("status", License.STATUS_DISABLED);
		getSqlSession().update("updateStatus", param);
	}
	
	public void enable(int id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("status", License.STATUS_IDLE);
		getSqlSession().update("updateStatus", param);
	}
	
	public void expired(int id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("status", License.STATUS_EXPIRED);
		getSqlSession().update("updateStatus", param);
	}
	
	public void deliver(List<License> licenses) {
		for (License license:licenses) {
			Map<String, Object> param = new HashMap<>();
			param.put("id", license.getId());
			param.put("license", license.getLicense());
			getSqlSession().update("deliver", param);
		}
	}
	
	public void activate(int id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("status", License.STATUS_ACTIVE);
		getSqlSession().update("activate", param);
	}
	
	public void refreshLastCheckTime(int id) {
		getSqlSession().update("refreshLastCheckTime", id);
	}
}